<template>
    <div>
        <maintainDetail v-if="detailVisible" @changeDetailVisible="changeDetailVisible" :effPlanDetail="effDetail">
        </maintainDetail>

        <addMaintainPlan v-else-if="addVisible" @addMaintain="addMaintain" :mainDevice="deviceList"
            :factoryList="companyList" :planPositionList="positionList" :planClassifyList="classifyList"
            @handlePlanOk="handlePlanOk" :editId="id">
        </addMaintainPlan>

        <div class="project zy-content" v-show="!detailVisible && !addVisible">
            <efficacious ref="efficacious" :planIds="ids" @addMaintain="addMaintain"
                @changeDetailVisible="changeDetailVisible" :device="deviceList" :effPlanList="effList"
                :effPlanTotal="effTotal">
                <Button slot="btns" @click="addMaintain(true)">{{
                    $t('maintenance.NewPlan') }}</Button>
            </efficacious>
        </div>
    </div>
</template>

<script>
import efficacious from './modules/efficacious.vue'
import addMaintainPlan from './modules/addMaintainPlan.vue'
import maintainDetail from './modules/maintainDetail.vue'

export default {
    name: 'maintainPlanPage',
    components: {
        efficacious,
        addMaintainPlan,
        maintainDetail
    },
    data() {
        return {
            id: '',
            ids: [],
            status: 0,
            effTotal: 0,
            cycleList: [
                { name: "全部", key: "" },
                { name: '小时', key: '8' },
                { name: "班次", key: "7" },
                { name: '日', key: '1' },
                { name: '周', key: '2' },
                { name: '月', key: '3' },
                { name: '季度', key: '4' },
                { name: '半年', key: '5' },
                { name: '年', key: '6' },
            ],
            invalidTotal: 0,
            addVisible: false,
            detailVisible: false,
            effDetail: {},
            checkBtnPermission: {},
            deviceList: [],
            companyList: [],
            positionList: [],
            classifyList: [],
            effList: [],
            invalidList: [],

        }
    },
    activated() {
        // this.searchList()
    },
    created() {
        // this.getDeviceList()
        this.getCompanyList()
        // this.getPositionList()
        // this.getClassifyList()
        this.searchList()
    },
    mounted() {
    },
    methods: {
        /**************基础数据**********************/
        getDeviceList() {
            this.axios({
                method: 'get',
                url: '/dms/dmsCategory/getSelectList',
                params: {}
            }).then(res => {
                if (res.code === 200) {
                    this.deviceList = res.data.map(item => {
                        return {
                            ...item,
                            value: item.value.toString()
                        }
                    })
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        //公司列表
        getCompanyList() {
            this.axios({
                method: 'get',
                url: '/basic/company/getCompanyListByUser',
                params: {}
            }).then(res => {
                if (res.code === 200) {
                    this.companyList = res.data
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        getPositionList() {
            this.axios({
                method: 'get',
                url: '/dms/dcmMaintainPosition/getSelectList',
                params: {}
            }).then(res => {
                if (res.code === 200) {
                    this.positionList = res.data
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        getClassifyList() {
            this.axios({
                method: 'get',
                url: '/dms/dcmMaintainClassify/getSelectList',
                params: {}
            }).then(res => {
                if (res.code === 200) {
                    this.classifyList = res.data
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        tabsClick(name) {
            console.log(name)
            this.status = name
            this.ids = []
            this.searchList()
            // if (name === 0) {
            //     this.$refs.productConfigComponent.searchBtn();
            // } else  if (name === 1){
            //     console.log(this.$$refs);
            //     this.$refs.boxConfigComponent.searchBtn();
            // }else{
            //     this.$refs.shfitconfigComponent.searchBtn();
            // }
        },
        searchList() {
            let data = {
                pageNum: 1,
                pageSize: 10,
            };
            this.axios({
                method: 'POST',
                url: '/dms/servicingPlan/getPage',
                data
            }).then(res => {
                if (res.code === 200) {
                    if (this.status == 0) {
                        this.effList = res.data.records
                        this.effTotal = res.data.total
                    } else {
                        this.invalidList = res.data.list
                        this.invalidTotal = res.data.total
                    }
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        handlePlanOk() {
            this.addVisible = false

            if (this.status == 0) {
                this.$refs.efficacious.searchList()
            } else {
                this.$refs.invalid.searchList()
            }

        },
        addMaintain(flag, record) {
            console.log(record,flag);
            let obj = record ? record : {}
            this.addVisible = flag
            if (obj.id) {
                this.id = record
            } else {
                this.id = ''
            }
        },
        changeDetailVisible(flag, id) {
            this.detailVisible = flag
            this.getEffDetail(id)
            if (!flag) {
                if (this.status == 0) {
                    this.$refs.efficacious.searchList()
                } else {
                    this.$refs.invalid.searchList()
                }
            }
        },
        getEffDetail(id) {
            this.axios({
                method: 'post',
                url: '/dms/servicingPlan/getByIdList',
                data: {
                    id
                }
            }).then(res => {
                if (res.code === 200) {
                    this.effDetail = res.data
                    const obj = this.cycleList.find(val => val.key == this.effDetail.triggerCycle)
                    if (obj) {
                        this.effDetail.triggerCycleText = res.data.intervalRate + obj.name
                    }
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
    },
}
</script>

<style lang="scss">
.project {

    .title {

        .icon {
            display: flex;

            .creatBtnright {
                display: flex;
                align-items: center;
                font-size: 16px;

                i {
                    font-size: 36px;
                }
            }
        }
    }
    .ivu-tabs-tab {
        font-size: 18px;
    }
}
</style>
